package com.zmx.common.common.utils;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Description: Http请求发送数据
 * @ClassName: HttpConnectionUtil
 * @Author zhaomxr
 * @Date 2021-09-06 14:39
 */
@Slf4j
public class HttpConnectionUtil {

    /**
     * 打开URLConnection
     *
     * @param path
     * @return
     */
    public static URLConnection urlConnection(String path) {
        URLConnection connection = null;

        try {
            //打开URLConnection进行资源读取
            URL url = new URL(path);
            connection = url.openConnection();
            if (connection instanceof HttpURLConnection) {
                //如果是HttpURLConnection设置块大小
                ((HttpURLConnection) connection).setChunkedStreamingMode(2048);
            }
            //超时时间
            connection.setConnectTimeout(10000);
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestProperty("content-type", "application/x-java-serialized-object,charset=utf-8");
        } catch (IOException e) {
            log.info("create URLConnection fail! path:{}", path);
            e.printStackTrace();
        }
        return connection;
    }

    public static JSONObject send(Object data, String path) {
        return send(data, urlConnection(path));
    }

    public static JSONObject send(Object data, URLConnection connection) {
        ObjectOutputStream out = null;
        ObjectInputStream in = null;

        OutputStream outputStream = null;
        InputStream inputStream = null;

        //转为json字符串
        String jsonStr = JacksonUtil.toJsonString(data);

        try {
            outputStream = connection.getOutputStream();
            out = new ObjectOutputStream(outputStream);
            out.writeObject(jsonStr);
            out.flush();

            inputStream = connection.getInputStream();
            in = new ObjectInputStream(inputStream);
            return JSONObject.fromObject(in.readObject());
        } catch (IOException | ClassNotFoundException e) {
            log.info("Http send data to server fail! data:{} path:{}", data, connection.getURL().getPath());
            e.printStackTrace();
        } finally {
            closeStream(out, in, outputStream, inputStream);
        }

        return null;
    }

    /**
     * 关闭流
     *
     * @param out
     * @param in
     * @param outputStream
     * @param inputStream
     */
    private static void closeStream(ObjectOutputStream out, ObjectInputStream in, OutputStream outputStream, InputStream inputStream) {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
